package ro.msg.office.beans;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Set;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;

import ro.msg.office.ticketmanager.DO.TicketDO;
import ro.msg.office.ticketmanager.DO.UserDO;
import ro.msg.office.ticketmanager.back.ifc.TicketManager;

/**
 * @author nikoran 
 * This class is created to represent the managed bean for the viewTicketsUser.xhtml page. It contains
 * the list of tickets read from database, which will be displayed in a table on the above page.
 */
public class ViewTicketsUserBean {

    /**
     * Is an array containing all the tickets of the application.
     */
    private TicketDO[] list;

    /**
     * Represents the length of the array, the number of tickets of the application.
     */
    private Integer listLength;

    /**
     * Verifies if the list has tickets or not. Represents the condition for making disabled the "view tickets" button
     * if the logged user has no tickets.
     */
    private Boolean isNull;

    /**
     * Represents the flag to disable the "ReSend Button" in the case of "Rejected" / "Accepted" / "Pending" ticket cases.
     */
    private Boolean isNotUpdate;

    /**
     * Represents the flag of the "Feedback" tag to appear/disappear. This tag appears only in the case of the
     * "Rejected" tickets.
     */
    private static String feedbackCase;

    /**
     * Represents the flag of the "Response Date" tag to appear/disappear. This tag appears only in the case of the
     * "Accepted" / "Rejected" / "More Details" tickets.
     */
    private static String responseDateCase;

    /**
     * This is the flag which sets the "Save Button" on the detailsUser.xhtml page to appear or disappear. It appears
     * only in the case of "Draft" tickets.
     */
    private static String saveCase;

    /**
     * This table is a copy of the one on the viewTicketsUser.xhtml page, which contains the tickets of the logged user.
     */
    private HtmlDataTable datatableTickets;

    /**
     * Represents the user which has signed in. This will be sent as a parameter to retrieve the tickets of this user.
     */
    private UserDO userDO;

    /**
     * This is an interface of the TicketManagerBean which contains the method of retrieving the tickets of the logged
     * user from the database.
     */
    @EJB
    TicketManager ticketEJB;

    /**
     * 
     * Returns the representation of the datatable from the viewTicketsUser.xhtml page.
     * 
     * @return - the representation of the datatable from the viewTicketsUser.xhtml page.
     */
    public HtmlDataTable getDatatableTickets() {
        return datatableTickets;
    }

    /**
     * 
     * Sets the value of the representation of the datatable from the viewTicketsUser.xhtml page, to be the given parameter.
     * 
     * @param datatableTickets - this will be the new value of the representation of the datatable.
     */
    public void setDatatableTickets(HtmlDataTable datatableTickets) {
        this.datatableTickets = datatableTickets;
    }

    /**
     * @return - This tells that the list of the tickets is empty or not.
     */
    public Boolean getIsNull() {
        return isNull;
    }

    /**
     * @param isNull - This sets the boolean variable to the given parameter.
     */
    public void setIsNull(Boolean isNull) {
        this.isNull = isNull;
    }

    /**
     * @return - This returns the list of the tickets of the logged user.
     */
    public TicketDO[] getList() {

        if (LoginBean.getSigned()) {
            userDO = new UserDO();

            UserDO userLoginDO = LoginBean.getUser();

            userDO.setUsername(userLoginDO.getUsername());
            userDO.setPassword(userLoginDO.getPassword());

            Set<TicketDO> tickets = ticketEJB.receiveTickets(userDO);

            TicketDO[] listTickets = (TicketDO[]) tickets.toArray(new TicketDO[0]);

            Arrays.sort(listTickets, new Comparator<TicketDO>() {

                public int compare(TicketDO t1, TicketDO t2) {
                    Date d1 = t1.getCreationDate();
                    Date d2 = t2.getCreationDate();
                    if (d1.after(d2))
                        return -1;
                    else if (d1.before(d2))
                        return 1;
                    return 0;
                }
            });

            setList(listTickets);
            setListLength(tickets.size());
        }

        return list;
    }

    /**
     * @param list - This sets the list of the tickets to be the given parameter.
     */
    public void setList(TicketDO[] list) {
        this.list = list;
    }

    /**
     * @return - This returns the length of the ticket list.
     */
    public Integer getListLength() {
        return listLength;
    }

    /**
     * @param listLength - This sets the length of the ticket list to be the given parameter.
     */
    public void setListLength(Integer listLength) {
        this.listLength = listLength;
    }

    /**
     * @return - This returns the variable that tells us if it is an update case or not. The "ReSend Button" is
     * available only in the case of "More Details" and "Draft" cases.
     */
    public Boolean getIsNotUpdate() {
        return isNotUpdate;
    }

    /**
     * @param isNotUpdate - This sets the variable that tells us if it is an update case, to be the given parameter.
     */
    public void setIsNotUpdate(Boolean isNotUpdate) {
        this.isNotUpdate = isNotUpdate;
    }

    /**
     * @return - This returns the flag needed for appearing / disappearing the "Save Button".
     */
    public String getSaveCase() {
        return saveCase;
    }

    /**
     * @param saveCasee - This flag is set to have the parameter value.
     */
    public static void setSaveCase(String saveCasee) {
        saveCase = saveCasee;
    }

    /**
     * @return - This method retrieves the details of the chosen ticket from the datatable, and sets the flags which are
     * used to manage the appearance of that data on the detailsUser.xhtml page.
     */
    public String viewTicketDetails() {

        String navigate = "";

        TicketDO ticketDO = (TicketDO) datatableTickets.getRowData();
        DetailsUserBean.setTicketDO(ticketDO);

        if (ticketDO.getStatusString().equals("rejected"))
            feedbackCase = "visibility:visible;";
        else
            feedbackCase = "visibility:hidden;";

        if (ticketDO.getStatusString().equals("accepted") || ticketDO.getStatusString().equals("rejected")
                || ticketDO.getStatusString().equals("more details"))
            responseDateCase = "visibility:visible;";
        else
            responseDateCase = "visibility:hidden;";

        if (ticketDO.getStatusString().equals("more details") || ticketDO.getStatusString().equals("draft")) {
            setIsNotUpdate(false);
        } else {
            setIsNotUpdate(true);
        }

        if (ticketDO.getStatusString().equals("draft")) {
            saveCase = "visibility:visible;";
        } else {
            saveCase = "visibility:hidden;";
        }

        navigate = "detailsUser";
        return navigate;
    }

    /**
     * @return - This function is used to check if the user has already tickets in the system. If not the
     * "View Tickets Button" will be disabled and a message will appear: "This user has no tickets!!!".
     */
    public String verifyListNotNull() {

        userDO = new UserDO();

        UserDO userLoginDO = LoginBean.getUser();

        userDO.setUsername(userLoginDO.getUsername());
        userDO.setPassword(userLoginDO.getPassword());

        Set<TicketDO> tickets = ticketEJB.receiveTickets(userDO);

        if (tickets.isEmpty()) {
            FacesMessage message = new FacesMessage("This user has no tickets!!!");
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, message);

            setIsNull(true);
            return "listNull";
        } else {
            setIsNull(false);
            return "viewTickets";
        }

    }

    /**
     * @return - This returns the flag of the "Response Date" tag.
     */
    public String getResponseDateCase() {
        return responseDateCase;
    }

    /**
     * @param responseDateCasee - This sets the flag of the "Response Date" tag to be the given parameter.
     */
    public static void setResponseDateCase(String responseDateCasee) {
        responseDateCase = responseDateCasee + ";";
    }

    /**
     * @return - This returns the flag of the "Feedback" tag.
     */
    public String getFeedbackCase() {
        return feedbackCase;
    }

    /**
     * @param feedbackCasee - This sets the flag of the "Feedback" tag to be the given parameter.
     */
    public static void setFeedbackCase(String feedbackCasee) {
        feedbackCase = feedbackCasee + ";";
    }

}
